package com.financing.platform.task;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.financing.platform.common.BizEnums;
import com.financing.platform.dto.FetchInformationDTO;
import com.financing.platform.dto.FpShopInformationDTO;
import com.financing.platform.service.IFetchInformationService;
import com.financing.platform.service.IFpShopInformationService;

@Component(value="fecthZixunJob")
public class FecthZixunJob {

	Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private IFetchInformationService fetchInformationService;
	@Autowired
	private IFpShopInformationService fpShopInformationService;

	//即时资讯抓取 2分钟抓取一次
	@Scheduled(cron = "0 0/2 * * * ?") 
	public void checke(){
		try {
			//查询最新一条即时资讯
			FpShopInformationDTO fpShopInformation = new FpShopInformationDTO();
			fpShopInformation.setType(BizEnums.ZixunEnums.IMMEDIATE);
			FpShopInformationDTO  fsiDto = fpShopInformationService.queryListGetFirst(fpShopInformation);

			//抓取大于最新一条咨询发布时间的咨询信息
			FetchInformationDTO fetchInformation = new FetchInformationDTO();
			if(fsiDto!=null && fsiDto.getReleaseTime() != null) {
				fetchInformation.setCreateDate(fsiDto.getReleaseTime());
			}
			List<FetchInformationDTO> list = fetchInformationService.queryFetchInfoList(fetchInformation);

			//保存抓取数据
			if(list.size()>0) {
				for(FetchInformationDTO f:list) {
					//查询是否已插入过此即时资讯 
					fpShopInformation = new FpShopInformationDTO();
					fpShopInformation.setType(BizEnums.ZixunEnums.IMMEDIATE);
					fpShopInformation.setTitle(f.getFetchTitle());
					FpShopInformationDTO fsiDto_isExist = fpShopInformationService.queryListGetFirst(fpShopInformation);
					
					//保存
					if(fsiDto_isExist.getId() == null) {
						fpShopInformation = new FpShopInformationDTO();
						fpShopInformation.setTitle(f.getFetchTitle());//标题
						fpShopInformation.setBirefDesc(f.getFetchTitle());//简介
						fpShopInformation.setContent(f.getFetchContent());//内容
						fpShopInformation.setType(BizEnums.ZixunEnums.IMMEDIATE);//咨询类型-即时资讯
						fpShopInformation.setReleaseTime(f.getCreateDate());//发布时间
						fpShopInformation.setUpStatus("1");//是否上架 是
						fpShopInformation.setPushMeg("2");//是否推送消息 否
						fpShopInformation.setCreateBy("system");
						fpShopInformation.setCreateDate(new Date());
						fpShopInformationService.insert(fpShopInformation);
					}
				}
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
